FROM openfaas/of-watchdog:0.8.1 as watchdog
FROM node:12.13.0-alpine as ship

COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
RUN chmod +x /usr/bin/fwatchdog

WORKDIR /root/

# Turn down the verbosity to default level.
ENV NPM_CONFIG_LOGLEVEL warn

RUN mkdir -p /home/app/
WORKDIR /home/app
COPY package.json ./

# This ordering means the npm installation is cached for the outer function handler.
RUN npm config set registry https://registry.npm.taobao.org
RUN npm i --production
RUN npm install express
RUN npm install body-parser
RUN npm install uuid
RUN npm install generic-pool
RUN npm install redis
RUN npm install axios
RUN npm install crypto-js
RUN npm install moment
RUN npm install lodash.isempty
RUN npm install lodash.isfinite
RUN npm install lodash.isplainobject
RUN npm install lodash.isequal

# Copy outer function handler
COPY index.js ./
COPY localtime /etc/
COPY function ./function

# chmod for tmp is for a buildkit issue (@alexellis)
RUN chmod +rx -R ./function && chmod 777 /tmp

ENV cgi_headers="true"
ENV fprocess="node index.js"
ENV mode="http"
ENV upstream_url="http://127.0.0.1:3000"

HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1

# ENTRYPOINT ["/home/app/start.sh"]
CMD ["fwatchdog"]
